﻿using B.S.InOut.Domain;
using B.S.InOut.Read.Api.Application.Command;
using B.S.InOut.Read.API.Application.Command;
using B.S.InOutErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.InOut.Read.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TranManagementController : ControllerBase
    {
        private readonly IMediator _mediator;

        public TranManagementController(IMediator mediator)
        {
            _mediator = mediator;
        }

        // 插入移库记录的接口方法
        [HttpPost]
        public async Task<IActionResult> InsertTranManagement([FromBody] Tranmanagement product)
        {
            var command = new InsertTranManagementCommand(product);
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        // 根据 ID 获取移库记录的接口方法
        [HttpGet]
        public async Task<IActionResult> GetTranManagementById(int id)
        {
            var query = new GetTranManagementByIdQuery(id);
            var result = await _mediator.Send(query);
            if (result == null)
            {
                return NotFound();
            }
            return Ok(result);
        }

        // 根据全部移库数据
        [HttpGet]
        public async Task<IActionResult> GetTranManagementQueryAll(string? TransferNum,int? Transfer,int? TranAuditstatus, int pageindex, int pagesize)
        {
            Apipaging<Tranmanagement> result = new Apipaging<Tranmanagement>();

            var query = new GetTranManagementAllQuery(TransferNum, Transfer, TranAuditstatus, pageindex, pagesize);
            var trans = await _mediator.Send(query);
            result.totalcount = trans.Count();
            result.totalpage = (int)Math.Ceiling(result.totalcount * 1.0 / pagesize);
            result.List = trans.OrderByDescending(p => p.Id).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
            if (result == null)
            {
                return NotFound();
            }
            return Ok(result);
        }

        // HTTP DELETE 请求，根据 ID 删除产品
        [HttpPost]
        public async Task<IActionResult> DeletTranmanage(int id)
        {
            // 创建删除产品命令
            var command = new DeleteTranmanageCommand(id);
            // 发送命令给中介者并获取受影响的行数
            var rowsAffected = await _mediator.Send(command);
            if (rowsAffected == 0)
            {
                // 如果没有记录受影响，返回 404 状态码
                return NotFound();
            }
            // 返回删除成功信息
            return Ok("删除成功");
        }

        /// <summary>
        /// 批量删除产品的 HTTP DELETE 方法
        /// </summary>
        /// <param name="productIds">要删除的产品 ID 列表</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<IActionResult> DeleteTranmanageALL([FromBody] IEnumerable<int> productIds)
        {
            var command = new DeleteTranmanageCommandALL(productIds);
            var rowsAffected = await _mediator.Send(command);

            if (rowsAffected > 0)
            {
                return Ok("成功");
            }
            else
            {
                return NotFound("未找到该数据");
            }
        }

        // HTTP PUT 请求，更新产品信息
        [HttpPost]
        public async Task<IActionResult> UpdateTranmanage([FromBody] Tranmanagement product)
        {
            // 创建更新产品命令
            var command = new UpdateTranmanageCommand(product);
            // 发送命令给中介者并获取受影响的行数
            var rowsAffected = await _mediator.Send(command);
            if (rowsAffected == 0)
            {
                // 如果没有记录受影响，返回 404 状态码
                return NotFound();
            }
            // 返回更新成功信息
            return Ok("更新成功");
        }

    }
}
